<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓货序列" prop="stockId">
        <el-input
          v-model="queryParams.stockId"
          placeholder="请输入仓货序列"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类别" prop="categoryId">
        <el-select
          v-model="queryParams.categoryId"
          placeholder="请选择类型"
          clearable
        >
          <el-option
            v-for="item in this.categoryList"
            :key="item.categoryId"
            :label="item.categoryName"
            :value="item.categoryId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否显示" prop="isShow">
        <el-select
          v-model="queryParams.isShow"
          placeholder="请选择是否显示"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_goods_show"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:goods:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:goods:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:goods:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:goods:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="goodsList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品的编号" align="center" prop="goodsId" />
      <el-table-column label="商品名称" align="center" prop="goodsName" />
      <el-table-column label="仓货序列" align="center" prop="stockId" />
      <el-table-column label="商品的库存" align="center" prop="quantity" />
      <el-table-column
        label="商品的图示"
        align="center"
        prop="goodsPicture"
        width="100"
      >
        <template slot-scope="scope">
          <image-preview
            :src="scope.row.goodsPicture"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>
      <el-table-column label="类别" align="center" prop="categoryName" />
      <el-table-column label="商品的价钱" align="center" prop="price" />
      <el-table-column label="商品的属性" align="center" prop="attribute">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="dialogDataVisibleHandler(scope.row)"
            >查看</el-button
          >
          <!-- weight: undefined,
        date: null,
        production: undefined,
        isMySelf: 2,
        shelflife: undefined, -->
          <el-dialog title="收货地址" :visible.sync="dialogDataVisible">
            <el-table :data="attributeList">
              <el-table-column property="date" label="日期"></el-table-column>
              <el-table-column property="weight" label="重量"></el-table-column>
              <el-table-column
                property="production"
                label="地址"
              ></el-table-column>
              <el-table-column
                property="isMySelf"
                label="是否未沃尔玛商品"
              ></el-table-column>
              <el-table-column
                property="shelflife"
                label="保质时长"
              ></el-table-column>
            </el-table>
          </el-dialog>
          <el-button
            size="mini"
            type="text"
            @click="dialogFormVisibleHandler(scope.row)"
            >修改</el-button
          >
          <el-dialog
            v-bind="$attrs"
            v-on="$listeners"
            @open="onOpen"
            @close="onClose"
            :visible.sync="dialogFormVisible"
            title="Dialog Title"
          >
            <el-row :gutter="15">
              <el-form
                ref="elForm"
                :model="formData"
                :rules="rules"
                size="medium"
                label-width="100px"
              >
                <el-col :span="12">
                  <el-form-item label="重量" prop="weight">
                    <el-input
                      v-model="formData.weight"
                      placeholder="请输入商品的重量重量"
                      clearable
                      :style="{ width: '100%' }"
                    >
                      <template slot="append">克</template>
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="生产日期" prop="date">
                    <el-date-picker
                      v-model="formData.date"
                      format="yyyy-MM-dd"
                      value-format="yyyy-MM-dd"
                      :style="{ width: '100%' }"
                      placeholder="请选择生产日期"
                      clearable
                    ></el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="产地" prop="production">
                    <el-input
                      v-model="formData.production"
                      placeholder="请输入产地"
                      clearable
                      :style="{ width: '100%' }"
                    >
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item
                    label-width="121px"
                    label="是否本家产品"
                    prop="isMySelf"
                  >
                    <el-radio-group v-model="formData.isMySelf" size="medium">
                      <el-radio
                        v-for="(item, index) in isMySelfOptions"
                        :key="index"
                        :label="item.value"
                        :disabled="item.disabled"
                        >{{ item.label }}</el-radio
                      >
                    </el-radio-group>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="保质时常" prop="shelflife">
                    <el-input
                      v-model="formData.shelflife"
                      placeholder="请输入保质时常"
                      clearable
                      :style="{ width: '100%' }"
                    >
                      <template slot="append">月</template>
                    </el-input>
                  </el-form-item>
                </el-col>
              </el-form>
            </el-row>
            <div slot="footer">
              <el-button @click="close">取消</el-button>
              <el-button type="primary" @click="handleConfirm">确定</el-button>
            </div>
          </el-dialog>
        </template>
      </el-table-column>
      <el-table-column label="是否显示" align="center" prop="isShow">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_goods_show"
            :value="scope.row.isShow"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:goods:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:goods:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品名称" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品的库存" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入商品的库存" />
        </el-form-item>
        <el-form-item label="仓货序列" prop="stockId">
          <el-input v-model="form.stockId" placeholder="请输入仓货序列" />
        </el-form-item>
        <el-form-item label="商品的图示">
          <image-upload v-model="form.goodsPicture" />
        </el-form-item>
        <el-form-item label="类别" prop="categoryId">
          <el-select
            v-model="form.categoryId"
            placeholder="请选择类型"
            clearable
          >
            <el-option
              v-for="item in this.categoryList"
              :key="item.categoryId"
              :label="item.categoryName"
              :value="item.categoryId"
            />
          </el-select>
          <!-- <el-input v-model="form.categoryId" placeholder="请输入类别" /> -->
        </el-form-item>
        <el-form-item label="商品的价钱" prop="price">
          <el-input v-model="form.price" placeholder="请输入商品的价钱" />
        </el-form-item>
        <el-form-item label="是否显示" prop="isShow">
          <el-select v-model="form.isShow" placeholder="请选择是否显示">
            <el-option
              v-for="dict in dict.type.sys_goods_show"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listGoods,
  getGoods,
  delGoods,
  addGoods,
  updateGoods,
} from "@/api/system/goods";
import { listCategory } from "@/api/system/category";

export default {
  name: "Goods",
  dicts: ["sys_goods_show"],
  data() {
    return {
      formLabelWidth: "120px",
      dialogDataVisible: false,
      dialogFormVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 类型选项
      categoryList: [],
      // 总条数
      total: 0,
      // 商品列表表格数据
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsName: null,
        stockId: null,
        quantity: null,
        goodsPicture: null,
        categoryId: null,
        price: null,
        isShow: null,
      },
      attributeform: {},
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      isMySelfOptions: [
        {
          label: "是",
          value: 1,
        },
        {
          label: "不是",
          value: 2,
        },
      ],

      formData: {
        weight: undefined,
        date: null,
        production: undefined,
        isMySelf: 2,
        shelflife: undefined,
      },
      attributeList: [],
      goodsId: 0,
    };
  },
  created() {
    this.getList();
    this.getCategoryList();
  },
  methods: {
    dialogFormVisibleHandler(row) {
      this.dialogFormVisible = true;
      this.goodsId = row.goodsId;
    },
    dialogDataVisibleHandler(row) {
      const d = row.attribute;

      const list = [];
      list.push(JSON.parse(d));
      console.log(list);
      this.attributeList = list;

      this.dialogDataVisible = true;
    },
    // 获取 商品类别参数
    getCategoryList() {
      this.loading = true;
      listCategory({
        pageNum: 1,
        pageSize: 10000,
      }).then((response) => {
        console.log(response.data);
        this.categoryList = response.data;
        this.loading = false;
      });
    },

    /** 查询商品列表列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then((response) => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        goodsId: null,
        goodsName: null,
        stockId: null,
        quantity: null,
        goodsPicture: null,
        categoryId: null,
        createTime: null,
        updateTime: null,
        price: null,
        isShow: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.goodsId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const goodsId = row.goodsId || this.ids;
      getGoods(goodsId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品列表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.goodsId != null) {
            updateGoods(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoods(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const goodsIds = row.goodsId || this.ids;
      this.$modal
        .confirm('是否确认删除商品列表编号为"' + goodsIds + '"的数据项？')
        .then(function () {
          return delGoods(goodsIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/goods/export",
        {
          ...this.queryParams,
        },
        `goods_${new Date().getTime()}.xlsx`
      );
    },
    onOpen() {},
    onClose() {
      this.$refs["elForm"].resetFields();
    },
    close() {
      this.dialogFormVisible = false;

      this.$emit("update:visible", false);
    },
    handleConfirm() {
      this.$refs["elForm"].validate((valid) => {
        if (!valid) return;
        const goodsId = this.goodsId;

        updateGoods({
          goodsId: goodsId,
          attribute: JSON.stringify(this.formData),
        }).then((response) => {
          this.$modal.msgSuccess("修改成功");
          this.dialogFormVisible = false;
          this.getList();
        });
      });
    },
  },
};
</script>
